*******************************************************************************
* Load data
*******************************************************************************

use dataset_voters, clear
keep if sample == "forthright"

*******************************************************************************
* Covariates
*******************************************************************************

local convictionrate convictionrate_3 convictionrate_4 ///
	convictionrate_5 convictionrate_6 convictionrate_7 convictionrate_8
local ageq ageq_2 ageq_3 ageq_4 ageq_5
local race race_2 race_3 race_4
local education education_2 education_3

#delimit;

local X 
	`convictionrate'
	`ageq'
	sex_2
	`race'
	`education'
	;

#delimit cr

*******************************************************************************
* Make interactions
*******************************************************************************

tabulate ideology, gen(ideology_cand_)
tabulate ideology_self, gen(ideology_self_)
tabulate distance, gen(distance_)
forvalues i = 1(1)7 {
	gen adaXideology_self_`i' = ada*ideology_self_`i'
	label variable ideology_cand_`i' "Ideology=`i'"
	label variable ideology_self_`i' "Ideology=`i'"
	label variable distance_`i' "Distance=`i'"
	label variable adaXideology_self_`i' "Experienced X Ideology=`i'"
}
drop ideology_cand_1 adaXideology_self_1 distance_1 ideology_self_1

*******************************************************************************
* First regression
*******************************************************************************

//keep if ideology_self > 0
gen index = _n
gen B = .
gen Bcf = .
gen SE = .
gen SEcf = .

xi: reg vote ada distance_* `X', cl(responseid)

local b_ada = _b[ada]
local ub_ada = _b[ada] + 1.96*_se[ada]
local lb_ada = _b[ada] - 1.96*_se[ada]

replace B = _b[ada] if index == 1
replace SE = _se[ada] if index == 1
su B
local myb = r(mean)

forvalues i = 2(1)7 {
	replace B = _b[distance_`i'] if index == `i'
	replace SE = _se[distance_`i'] if index == `i'
	replace Bcf = B + `myb' if index == `i'
	lincom distance_`i' + ada
	replace SEcf = r(se) if index == `i'
}

preserve

	drop if mi(B)
	
	collapse B SE Bcf SEcf, by(index)

	replace index = index - 1
	
	replace Bcf = B if index == 0
	replace SEcf = SE if index == 0
	replace B = 0 if index == 0
	
	gen UB = B + 1.96*SE if index > 0
	gen LB = B - 1.96*SE if index > 0
	gen UBcf = Bcf + 1.96*SEcf
	gen LBcf = Bcf - 1.96*SEcf

	#delimit;
	
	gr tw
		(rspike UB LB index, lwid(thick) col(black))
		(sc B index, msym(O) col(black))
		(rspike UBcf LBcf index, col(gray))
		(sc Bcf index, col(white) msym(O) mlcol(gray))
		,
			yline(0, lpat(dash))
			xlab(0(1)6)
			legend(off)
			plotregion(style(none))
			ylab(, angle(horiz))
			xtitle("Ideological Distance")
			ytitle("Effect on Vote Choice")
			text(0.15 1.9 "+Experience Effect", col(gray))
			;
	
	#delimit cr

restore

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figure5_a.pdf", replace

*******************************************************************************
* Second regression
*******************************************************************************

drop B SE index

gen B = .
gen SE = .
gen index = _n

reg vote ada ideology_self_* adaXideology_self* `X', cl(responseid)

replace B = _b[ada] if index == 1
replace SE = _se[ada] if index == 1

forvalues i = 2(1)7 {
	lincom ada + adaXideology_self_`i'
	replace B = r(estimate) if index == `i'
	replace SE = r(se) if index == `i'
}

preserve

	keep if !mi(B)

	collapse B SE, by(index)

	gen UB = B + 1.96*SE
	gen LB = B - 1.96*SE

	#delimit;

	gr tw
		(sc B index, col(black))
		(rspike UB LB index, col(black))
		,
			plotregion(style(none))
			ylab(, angle(horiz))
			xtitle("Respondent Conservatism")
			ytitle("Marginal Effect of Experience")
			xlab(1(1)7)
			yline(`b_ada', lcol(gray) lwid(thin))
			yline(`ub_ada' `lb_ada', lcol(gray) lpat(dash))
			text(0.075 4 "Full sample estimate", col(gray)
			size(small))
			legend(off)
			;
	#delimit cr
	
restore

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figure5_b.pdf", replace

*******************************************************************************
* End
*******************************************************************************


